package _2观察者模式.拉数据方式;

import java.util.ArrayList;

class ShopSubject implements Subject {
    private String goodsName;   // 商品名称
    private double oldPrice, newPrice;   // 原价、现价
    private ArrayList<Observer> customerList;   // 顾客列表

    ShopSubject() {
        customerList = new ArrayList<>();
        oldPrice = 0;
        newPrice = 0;
    }

    @Override
    public void addObserver(Observer observer) {
        if (!customerList.contains(observer)) {
            this.customerList.add(observer);
        }
    }

    @Override
    public void deleteObserver(Observer observer) {
        if (customerList.contains(observer)) {
            this.customerList.remove(observer);
        } else {
            System.out.println("顾客列表中没有该顾客！");
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : customerList) {
            observer.update();
        }
    }

    public void modifyGoods(String goodsName, double oldPrice, double newPrice) {
        this.goodsName = goodsName;
        this.oldPrice = oldPrice;
        this.newPrice = newPrice;
        notifyObserver();
    }

    public String getGoodsName() {
        return goodsName;
    }

    public double getOldPrice() {
        return oldPrice;
    }

    public double getNewPrice() {
        return newPrice;
    }
}

// 只关心优惠商品的名称
class CustomerOne implements Observer {
    private Subject subject;
    private String goodsName, customerName;

    CustomerOne(String customerName, Subject subject) {
        this.subject = subject;
        this.customerName = customerName;
        this.subject.addObserver(this);
    }

    @Override
    public void update() {
        // 收到通知，主动获取信息
        if (subject instanceof ShopSubject) {
            this.goodsName = ((ShopSubject) subject).getGoodsName();
            System.out.println(customerName + "只关心优惠商品名称：");
            System.out.println("打折商品是：" + goodsName);
        }
    }
}

// 只关心优惠商品的价格
class CustomerTwo implements Observer {
    private Subject subject;
    private String customerName;
    private double oldPrice,newPrice;

    CustomerTwo(String customerName, Subject subject) {
        this.subject = subject;
        this.customerName = customerName;
        this.subject.addObserver(this);
    }

    @Override
    public void update() {
        // 收到通知，主动获取信息
        if (subject instanceof ShopSubject) {
            this.oldPrice = ((ShopSubject) subject).getOldPrice();
            this.newPrice = ((ShopSubject) subject).getNewPrice();
            System.out.println(customerName + "只关心优惠商品价格：");
            System.out.println("打折商品的原价是：" + oldPrice);
            System.out.println("打折商品的现价是：" + newPrice);
        }
    }
}

public class Demo02 {

    public static void main(String[] args) {
        ShopSubject shopSubject = new ShopSubject();
        new CustomerOne("kary", shopSubject);
        new CustomerTwo("marry", shopSubject);
        shopSubject.modifyGoods("iPhone11 pro", 8889,7999);
    }
}
